<?php

define("VK_APP_ID", "3011711");
define("VK_APP_PASSWORD", "ra5DjIe92FPHg66M4r3m");

define('HTTP_SERVER', 'http://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\') . '/');
//define('HTTP_SERVER', 'http://localhost/b/');
//DEFINE DB OPTIONS DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE
//
//define("DB_DRIVER", "mysql");
//define("DB_HOSTNAME", "localhost");
//define("DB_USERNAME", "root");
//define("DB_PASSWORD", "123");
//define("DB_DATABASE", "pleasure");

//DEFINE DIRECTORIES
 
define('DIR_APPLICATION', 'application/');
define('DIR_APPCONFIG', 'config/');
define('DIR_MODULES', 'modules/');
define('DIR_TEMPLATES', 'templates/');
define('DIR_BASE',  '');
define('DIR_COMPONENTS',  'components/');
define('DIR_SYSTEM', 'core/');
define('DIR_DATABASE',  DIR_SYSTEM . 'db/');
define('DIR_LIBRARY',  DIR_SYSTEM . 'lib/');
define('DIR_ENGINE', DIR_SYSTEM . 'engine/');


// DEFINES APP CONFIG
function initAppConfig()
{
    $defmodules = array ();
    $usermodules = array ();
    $alloweddrivers = array('mysql');
    $fp = fopen(DIR_APPCONFIG . 'appconfig.cfg', 'r');
    while(!feof($fp))
    {
        $str = fgets($fp);
        if($str[0]!="#" && $str[0]!="")
        {
//            echo $str;
            // Read Database Config
            if(preg_match('/^(db).([\D]+)=([\s0-9a-zA-Z]+)$/ui', $str, $res))
//           $res[2] = название параметра (driver)
//           $res[3] = значение параметра   (mysql)
            {
                $param = trim($res[2]);
                $value = trim($res[3]);
                if($param == 'driver')
                {
                    if(!in_array(strtolower($value), $alloweddrivers))
                    {
                        trigger_error("Driver " . $value . " not available!");
                        exit();
                    }
                    else
                    {
                        define("DB_DRIVER", strtolower($value));  
                    }
                }
                else
                    define("DB_" . strtoupper($param), $value); 
//                echo 'DB_' . strtoupper($param) . ' = ' . $value;
            }
            
            // Read enabled modules
            if(preg_match('/^((?:md)|(?:mu)).([\D]+)$/ui', $str, $res))
            {
                switch($res[1])
                {
                    case 'md':
                        $defmodules [] = trim($res[2]);
                        break;
                    case 'mu':
                        $usermodules [] = trim($res[2]);
                        break;
                }
            }
        }
    }
    fclose($fp);
    return array(
        'defmodules' => $defmodules,
        'usermodules' => $usermodules
        );
}

function __autoload($className) 
{
    if (file_exists(DIR_LIBRARY . 'class.' . $className . '.php')) {
        require_once(DIR_LIBRARY . 'class.' . $className . '.php');
    } else
    if (file_exists(DIR_ENGINE . 'class.' . $className . '.php')) {
        require_once(DIR_ENGINE . 'class.' . $className . '.php');
    }
    else
    {
        echo "class ".$className." not found!";
        exit();
    }
}
?>
